package vn.demand.dragon.editor
{
	import flash.events.Event;
	
	import mx.controls.ComboBox;
	
	public class EditorComboBox extends ComboBox
	{
		[Bindable]
		private var __selectedData:int;
		
		public var keyField:String = 'data';
		
		public function EditorComboBox()
		{
			addEventListener(Event.CHANGE,onChange);
		}
		
		private function onChange(event:Event):void {
			__selectedData = event.currentTarget.selectedItem[keyField];
			RIALogger.debug(this, "EditorComboBox.selectedItem[" + keyField + "] = " + __selectedData);
		}
		
		override public function set selectedIndex(value:int):void
    	{
    		if (value < 0 || value >= dataProvider.length) {
    			return;
    		} 
    		super.selectedIndex = value;
    		if (value > -1) {
    			
	    		__selectedData = dataProvider[value][keyField];
    		}
    	}
		
		public function set selectedData(value:int):void {
			__selectedData = value;
			var index:int = 0;			
			for each(var item:Object in dataProvider) {
				if (!item.hasOwnProperty(keyField)) {
					break;
				}
				if (item[keyField] == value) {
					selectedIndex = index;
					__selectedData = value;
					return;
				}
				index++;
			}
			return;
		}
		
		public function get selectedData():int {
			return __selectedData;
		}

	}
}